﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.HoSoService;
namespace eTMS.Controllers{
public class HoSoControllerBase : CommonController{
	public HoSoControllerBase()
		: base()
	{	
		this.dao = new HoSoServiceClient();
	}
	~HoSoControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private HoSoEntity oHoSo;	
	public HoSoEntity HoSoObject{
		get{
			return oHoSo;
		}
		set{
			oHoSo=value;
		}
	}
	protected HoSoServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSHoSo(false);			
			return _Table;
		}
	}
	public void LoadAllDSHoSo(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oHoSo==null)return false;
		bool result=true;
		try{
			oHoSo = dao.SaveRet(oHoSo);
		}
		catch{
			result=false;
		}
		if(result){
			_ListHoSo.Add(oHoSo);
		}
		return result;
	}
	public bool Update(){
		if(oHoSo==null)return false;
		return dao.Update(oHoSo);
	}
	public bool Delete(){
		if(oHoSo==null)return false;
		bool retvalue = dao.Delete(oHoSo);
		if(retvalue){			
			_ListHoSo.Remove(oHoSo);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 HoSo_ID){
		bool retvalue = dao.DeleteByID(HoSo_ID);		
		if(retvalue){
			HoSoEntity oDeleted=FindHoSo(HoSo_ID);
			if(oDeleted!=null){
				_ListHoSo.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oHoSo = new HoSoEntity();
	}
	//Check if exists MaHoSo
	public bool CheckExistsMaHoSo(object value){	
		return dao.SelectOneByMaHoSo(value.ToString()).MaHoSo!=null;
    }    
    //Copy data from data row in grid to object
    public HoSoEntity MapDataRowToObject(DataRow row){
		HoSoEntity o = new HoSoEntity();
		 o.MaHoSo=Convert.ToString(row["MaHoSo"]);
		 o.TenHoSo=Convert.ToString(row["TenHoSo"]);
		 o.MoTaHoSo=Convert.ToString(row["MoTaHoSo"]);
		 if(!row.IsNull("NopKhiNhapHoc"))
			o.NopKhiNhapHoc=ConvertBool(row["NopKhiNhapHoc"]);
		 if(!row.IsNull("HoSo_ID"))
		    o.HoSo_ID=(Int32)(row["HoSo_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(HoSoEntity o,DataRow row){
		row["MaHoSo"]=o.MaHoSo;
		row["TenHoSo"]=o.TenHoSo;
		row["MoTaHoSo"]=o.MoTaHoSo;
		row["NopKhiNhapHoc"]=o.NopKhiNhapHoc;
		row["HoSo_ID"]=o.HoSo_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.HoSoObject,row);
    }
	public void LoadObject(Int32 HoSo_ID){
		HoSoEntity oLoad=FindHoSo(HoSo_ID);
		if(oLoad==null){
			oHoSo=this.LoadHoSo(HoSo_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oHoSo.IsNullHoSo_ID;
			if(!bNotIsNull){
				this.ListHoSo.Add(oHoSo);
			}
		}
		else{
			oHoSo=oLoad;
		}
	}
	#region CachedList HoSo
	private static List<HoSoEntity> _ListHoSo=new List<HoSoEntity>();
    public List<HoSoEntity>ListHoSo{
		get{
			return _ListHoSo;
		}
	}
	public static List<HoSoEntity>ListHoSoStatic{
		get{
			return _ListHoSo;
		}
	}
	public HoSoEntity FindHoSo(Int32 HoSo_ID){
		HoSoEntity oResult=this.ListHoSo.Find(delegate(HoSoEntity oFind){
			bool result=true; 
			result&=oFind.HoSo_ID.Equals(HoSo_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllHoSo(bool clearCache){
		if(_ListHoSo.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		HoSoServiceClient dao = new HoSoServiceClient();
		_ListHoSo=new List<HoSoEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public HoSoEntity LoadHoSo(Int32 HoSo_ID){			
		HoSoEntity o=dao.SelectOne(HoSo_ID);		
		return o;
	}
	#endregion
}
}
